$(function () {
    EditData();
    EditGetDetails();

});

// 提交修改的信息
function EditData() {
    $('#edit_user_details_button').click(function () {
        var nickname = $('#edit_user_nick_name').val();
        var sex = $('#edit_user_sex').val();
        var email = $('#edit_user_email').val();
        var address = $('#edit_user_address').val();
        var id = localStorage.id;   // 用了token验证了不需要用id了
        data = {
            'nickname': nickname,
            'sex': sex,
            'email': email,
            'address': address,
        };
        var token = localStorage.token || sessionStorage.token;

        $.ajax({
                url: host + "/edit_user_details/",
                method: "put",
                data: JSON.stringify(data),
                headers: {'Authorization': 'JWT ' + token},
                contentType: "application/json",
                xhrFields: {
                    withCredentials: true
                },
                success: function (resp) {
                    console.log(resp)
                    localStorage.nickname = resp.nickname
                    localStorage.sex = resp.sex
                    localStorage.email = resp.email
                    localStorage.address = resp.address
                    location.href = './personal_center.html';

                },
                error: function (resp) {
                    resp_data = JSON.parse(resp.responseText);  // 可以取到错误信息
                    if (resp_data.email){
                        $('#edit_user_email_1').html(resp_data.email)
                    }else {
                        alert('服务器异常！')
                    }

                }
            })
    })

}

// 修改资料获得资料
function EditGetDetails() {
    var nickname = localStorage.nickname || sessionStorage.nickname;
    var sex = localStorage.sex || sessionStorage.sex;
    var email = localStorage.email || sessionStorage.email;
    var address = localStorage.address || sessionStorage.address;
    console.log(nickname, sex, email, address)

    if (nickname == 'undefined'){
    }else {
        $('#edit_user_nick_name').val(nickname);
    }
    if (sex == 'undefined'){
    }else {
        $('#edit_user_sex').val(sex);
    }
    if (email == 'undefined'){
    }else {
        $('#edit_user_email').val(email);
    }
    if (address == 'undefined'){
    }else {
        $('#edit_user_address').val(address)
    }

}